<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }
        ul, li {
            list-style: none;
            display: inline-block;
        }
        body {
            display: flex;
            flex-direction: column;
            height: 100vh;
        }
        .menu {
            display: flex;
            justify-content: space-around;
            align-items: center;
            width: 100vw;
            height: 10vh;
            border-bottom: px solid #eee;
        }
        .menu li {
            display: flex;
            justify-content: center;
            align-items: center;
            width: 100px;
            height: 50px;
            border: 1px solid #eee;
        }
        #canvas {
            flex: 1;
        }
        .active {
            box-shadow: 0,0,20px,rgba(133, 51, 51, 0.3) !important;
            background: yellow;
        }
        .small::before {
            position: absolute;
            content: "";
            width: 6px;
            height: 6px;
            border-radius: 6px;
            background: #333;
        }
        .normal::before {
            position: absolute;
            content: "";
            width: 16px;
            height: 16px;
            border-radius: 16px;
            background: #333;
        }
        .large::before {
            position: absolute;
            content: "";
            width: 32px;
            height: 32px;
            border-radius: 32px;
            background: #333;
        }
    </style>
</head>
<body>
    <ul class="menu">
        <li class="btn" data-activeIndex="1">画笔</li>
        <li class="btn" data-activeIndex="2">矩形</li>
        <li class="btn line small"  data-activeIndex="3"></li>
        <li class="btn line normal"  data-activeIndex="4"></li>
        <li class="btn line large"  data-activeIndex="5"></li>
        <li class="btn"  data-activeIndex="6"><input type="color" id="color" value="" name="color"></li>
        <li class="btn  data-activeIndex="7"><a class="download" download href="">下载</a></li>
    </ul>
    <canvas id="canvas"></canvas>

    <script>
        var canvas = document.querySelector('canvas')
        var ctx = canvas.getContext('2d')
        // 设置画布宽高
        // console.log([canvas])
        canvas.setAttribute('width',  canvas.offsetWidth)
        canvas.setAttribute('height', canvas.offsetHeight)
        

        var huaban = {
            type: 'none',
            isDrag: false,
            beginX: 0,
            beginY: 0,
            imageData: null,
            lineWidth: 6,
            color: '#333',
            huabiFn: function(e) {
                var x = e.pageX - canvas.offsetLeft
                var y = e.pageY - canvas.offsetTop
                ctx.lineTo(x, y)
                ctx.fillStyle = 'black'
                ctx.lineCap = 'round'
                ctx.lineJoin = 'round'
                ctx.strokeStyle = huaban.color
                ctx.lineWidth = huaban.lineWidth
                // ctx.arc(x, y, 5, 0, 2*Math.PI)
                
                ctx.stroke()
            },
            rectFn: function(e) {
                var x = e.pageX - canvas.offsetLeft
                var y = e.pageY - canvas.offsetTop
               
                ctx.clearRect(0,0,canvas.offsetWidth, canvas.offsetHeight)
                // 因为这里初始化数据是null, 需要先做判断
                if (null !== huaban.imageData) {
                    // 1.需要加载的数据, 23.初始画布加载的初始坐标, 45.数据画布加载的初始坐标 56.画布的宽高
                    ctx.putImageData(huaban.imageData, 0,0,0,0,canvas.offsetWidth, canvas.offsetHeight)
                }
                ctx.beginPath()
                ctx.fillStyle=huaban.color
                
                // 这里的3,4参数是具体的宽高, 而不是坐标, 所以用减法
                ctx.fillRect(huaban.beginX, huaban.beginY, x-huaban.beginX, y-huaban.beginY)
                // ctx.stroke()
                ctx.closePath()
                console.log(2)
            }
        }

        // 事件委托
        // document.querySelector('.menu').addEventListener('click', function(e) {
        //     console.log(e.target.dataset.activeindex !== '1')
        //     if (e.target.dataset.activeIndex !== 1) {
        //         lines.forEach(item => {
        //             item.classList.remove('active')
        //         })
        //         e.target.classList.add('active')
        //     }
        // })

        

        // 点击切换高亮
        var liEle = document.getElementsByTagName('li')
        var liEleByClass = document.querySelectorAll('.btn')
        var lines = document.querySelectorAll('.line')
        
        // 初始化内容
        liEle[0].classList.add('active')
        lines[0].classList.add('active')
        huaban.type = 'huabi'

        // 使用a标签进行下载, a标签中需要加入download的标记, 否则会报错
        // 获取数据使用 canvas.toDataURL, 注意URL是大写
        document.querySelector('.download').onclick = function(e) {
            var imageUrl = canvas.toDataURL()
            this.setAttribute('href', imageUrl)
            // var iframe = "<iframe width='100%' height='100%' src='" + imageUrl +"'></iframe>";
            // var x = window.open();
            // x.document.open();
            // x.document.write(iframe);
            // x.document.close();

        }

        // console.log([document.querySelector('#color')])
        document.querySelector('#color').onchange = function(e) {
            huaban.color = e.target.value
            // console.log(e, e.value)
        }

        // console.log(liEle,liEleByClass)
        liEle[0].onclick = function(e) {
            liEleByClass.forEach((item, i) => {
                item.classList.remove('active')
            })
            this.classList.add('active')
            lines[0].classList.add('active')
            huaban.type = 'huabi'
        }
        liEle[1].onclick = function(e) {
            liEleByClass.forEach((item, i) => {
                item.classList.remove('active')
            })
            this.classList.add('active')
            huaban.type = 'rect'
        }
        liEle[2].onclick = function(e) {
            lines.forEach((item, i) => {
                item.classList.remove('active')
            })
            huaban.lineWidth = 6
            this.classList.add('active')
        }
        liEle[3].onclick = function(e) {
            lines.forEach((item, i) => {
                item.classList.remove('active')
            })
            huaban.lineWidth = 16
            this.classList.add('active')
        }
        liEle[4].onclick = function(e) {
            lines.forEach((item, i) => {
                item.classList.remove('active')
            })
            huaban.lineWidth = 32
            this.classList.add('active')
        }
        liEle[5].onclick = function(e) {
            liEleByClass.forEach((item, i) => {
                item.classList.remove('active')
            })
            this.classList.add('active')
        }



        // 拖拽事件
        canvas.onmousedown = function(e) {
            huaban.isDrag = true
            if (huaban.type === 'rect') {
                var beginX = e.pageX - canvas.offsetLeft
                var beginY = e.pageY - canvas.offsetTop

                huaban.beginX = beginX
                huaban.beginY = beginY
            }

            if (huaban.type === 'huabi') {
                ctx.beginPath()
                var beginX = e.pageX - canvas.offsetLeft
                var beginY = e.pageY - canvas.offsetTop

                huaban.beginX = beginX
                huaban.beginY = beginY
                ctx.moveTo(beginX, beginY)
            }
        }
        canvas.onmouseup = function(e) {
            huaban.isDrag = false
            huaban.imageData = ctx.getImageData(0,0,canvas.offsetWidth, canvas.offsetHeight)

            if (huaban.type === 'huabi') {
                ctx.closePath()
            }
            
        }
        canvas.onmousemove = function(e) {
            if (huaban.type === 'huabi' && huaban.isDrag) {
                huaban.huabiFn(e)
            }
            if (huaban.type === 'rect' && huaban.isDrag) {
                huaban.rectFn(e)
            }
        }
        </script>
</body>
</html>